package datastructure.linkedlist.loop;

import datastructure.common.AbstractList;

/**
 * 双向循环链表
 *
 * @author cyt
 * @create 2020-11-28 上午 11:33
 */
public class CyLoopLinkedList<E> extends AbstractList<E> {
    private Node first;
    private Node last;

    @Override
    public E remove(int index) {
        rangeCheck(index);
        Node<E> node = node(index);
        Node next = node.next;
        Node prev = node.prev;
        /**
         * 对删除首部、尾部元素做出特殊处理
         */
        if (prev == null) {
            first = next;
        } else {
            prev.next = next;
        }

        if (next == null) {
            last = node;
        } else {
            next.prev = prev;
        }
        size--;
        return node.element;
    }

    @Override
    public void clear() {
        size = 0;
        first = null;
        last = null;
    }

    @Override
    public E get(int index) {
        return node(index).element;
    }

    @Override
    public int indexOf(E element) {
        Node<E> node = first;
        if (element == null) {
            for (int i = 0; i < size; i++) {
                if (node.element == null) {
                    return i;
                }
                node = node.next;
            }
        } else {
            for (int i = 0; i < size; i++) {
                if (node.element.equals(element)) {
                    return i;
                }
                node = node.next;
            }
        }
        return ELEMENT_NOT_FOUND;
    }

    @Override
    public void add(int index, E element) {
        rangeCheckOfAdd(index);
        if (index == size) {
            Node<E> oldLast = last;
            last = new Node<>(element, oldLast, null);
            /**
             * 如果是第一次 添加元素
             * 头节点和尾节点都指向这个元素
             */
            if (oldLast == null) {
                first = last;
            } else {
                oldLast.next = last;
            }
        } else {
            Node<E> next = node(index);
            Node prev = next.prev;
            Node<E> node = new Node<>(element, prev, next);
            next.prev = node;
            /**
             * 此时向第一个位置加入
             */
            if (prev == null) {
                first = node;
            } else {
                prev.next = node;
            }
        }
        size++;
    }

    @Override
    public E set(int index, E element) {
        Node<E> node = node(index);
        E old = node.element;
        node.element = element;
        return old;
    }

    /**
     * 双向循环链表中寻找指定位置的元素
     *
     * @param index
     * @return
     */
    private Node<E> node(int index) {
        rangeCheck(index);
        Node node = first;
        if (index < (size >> 1)) {
            for (int i = 0; i <= index; i++) {
                node = node.next;
            }
        } else {
            node = last;
            for (int i = size; i > index; i--) {
                node = node.prev;
            }
        }
        return node;
    }

    private static class Node<E> {
        E element;
        Node next;
        Node prev;

        public Node(E elements, Node prev, Node next) {
            this.element = elements;
            this.next = next;
            this.prev = prev;
        }

        @Override
        public String toString() {
            StringBuilder builder = new StringBuilder();
            if (prev != null) {
                builder.append(prev.element);
            } else {
                builder.append("null");
            }
            builder.append("_").append(element).append("_");
            if (next != null) {
                builder.append(next.element);
            } else {
                builder.append("null");
            }
            return builder.toString();
        }
    }

    @Override
    public String toString() {
        Node<E> node = first;
        StringBuilder builder = new StringBuilder();
        builder.append("size = ").append(size).append("  [");
        for (Integer i = 0; i < size; i++) {
            builder.append(node);
            if (i != size - 1) {
                builder.append(",");
            }
            node = node.next;
        }
        builder.append("]");
        return builder.toString();
    }
}
